import consts from '../../js/const.js';
import util from '../../utils/util';
import {routerFilter} from '../../utils/routerFilter';
routerFilter({

  /**
   * 页面的初始数据
   */
  data: {
    itemBackgroundImageUrl:consts.getImageUrl('my-2.png'),
    settingImgUrl:consts.getImageUrl('setting.png'),
    orderImageUrl:consts.getImageUrl('order.png'),
    unpayImageUrl:consts.getImageUrl('daizhifu.png'),
    uncompleteImageUrl:consts.getImageUrl('uncomplete.png'),
    completeImageUrl:consts.getImageUrl('complete.png'),
    returnedImageUrl:consts.getImageUrl('returned.png'),
    userInfo: {},
    userId: util.getStorageSync(consts.cache_key.USER_ID),
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    vehicleFileCount:0,
    activeNames: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          this.setData({
            hasUserInfo: true
          })
        }
      }
    });
    let that=this;
    that.isLogin(null,function () {
      util.promise('/searchEycVehicleFileCountByUserId').then(rs=>{
        if(rs.data){
          that.setData({vehicleFileCount:rs.data});
        }
      });

      util.promise('/sumSavingCapital').then(rs=>{
        if(rs.data){
          that.setData({savingCapital:rs.data});
        }
      });
      util.promise('/sumVehicleAvAmount').then(rs=>{
        if(rs.data){
          that.setData({vehicleAvAmount:rs.data});
        }
      });
      util.promise('/hasVerificationAuthority').then(rs=>{
        if(rs.data && rs.data.length>0){
          let inspStationIds=util.map(rs.data,function (r){
            return r.inspStationOrShopId;
          })
          that.setData({inspStationIds:inspStationIds});
          that.setData({hasVerificationAuthority:true});
        }
      });
    });
    that.refreshOrderCount();

  },
  refreshOrderCount(){
    let that=this;
    util.promise('/searchUnpaidOrderCount').then(rs=>{
      if(rs.data!==null){
        that.setData({unpaidOrderCount:rs.data});
      }
    });
    util.promise('/searchUncompletedOrderCount').then(rs=>{
      if(rs.data!==null){
        that.setData({uncompletedOrderCount:rs.data});
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshOrderCount();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  selectVehicleFiles(event){
    let that = this;
    wx.navigateTo({
      url: '/pages/vehicle/vehicleFileItems',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptVehicleFileFromItemsPage: function (vehicleFile) {
          that.refreshVehicleFile(vehicleFile);
        }
      },
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', { isUpdate: true });
      }
    })
  },
  showOrderList(event){
    let that = this;
    let dataset=event.currentTarget.dataset;
    if(dataset.index){
      dataset.index=parseInt(dataset.index);
    }
    if(dataset.isVerification){
      dataset.isVerification= JSON.parse(dataset.isVerification);
      dataset.inspStationIds=that.data.inspStationIds;
    }
    wx.navigateTo({
      url: '/pages/order/orderList',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        refreshData: function () {
          that.refreshOrderCount();
        }
      },
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', dataset);
      }
    })
  },
  viewMessage(event){
    wx.navigateTo({
      url: '/pages/my/subscribeMessage',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        refreshData: function () {
         // that.refreshOrderCount();
        }
      },
      success: function (res) {
       // res.eventChannel.emit('acceptDataFromOpenerPage', dataset);
      }
    })
  },
  myCoupon(event){
    let typeCde=event.currentTarget.dataset.typeCde;
    let pageNme=event.currentTarget.dataset.pageNme;
    wx.navigateTo({
      url: '/pages/my/'+pageNme,
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        refreshData: function () {
          // that.refreshOrderCount();
        }
      },
      success: function (res) {
         res.eventChannel.emit('acceptDataFromOpenerPage');
      }
    })
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  }
});